package com.taptech.doufu.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.MineNovelBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.NovelCreateServices;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;

/**
 * Created by op0509 on 2016/5/17.
 */
public class NovelOriginalDialog extends Dialog implements View.OnClickListener, HttpResponseListener {
    private Context context;
    private TextView tvZhuzhan, tvShoufa;
    private ImageView imgZhuzhan, imgShoufa, imgHelper;
    private RelativeLayout rlZhuzhan, rlShoufa;
    private Button btnCancel, btnSure;
    private EditText etTongren;
    private MineNovelBean bean;
    private int typePublish, from;
    private String content, name, novelType;
    private OriginalTypeSelectListener listener;
    private WaitDialog waitDialog;

    public interface OriginalTypeSelectListener{
        void selectOriginalListener(String typeOriginal, int typeNum, String tongOrihinal);
    }

    public void setOnOriginalTypeSeleceListener(OriginalTypeSelectListener listener){
        this.listener = listener;
    }

    public NovelOriginalDialog(Context context) {
        super(context);
        this.context = context;
    }
    public NovelOriginalDialog(Context context, int theme, int typePublish, String content, MineNovelBean bean, int from) {
        super(context, theme);
        this.context = context;
        this.typePublish = typePublish;
        this.content = content;
        this.bean = bean;
        this.from = from;// 从哪个页面过来的(目前只做了从小说设置页面的区分)    3为小说设置，其他均为默认
        waitDialog = new WaitDialog(context, R.style.updateDialog, "处理中,请稍后...");
        if(this.bean != null){
            novelType = bean.getNovel_type();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_novel_create_original);

        initUI();
        selectPublish(typePublish);
        if(!TextUtils.isEmpty(content)){
            etTongren.setText(content);
            etTongren.requestFocus();
        }
    }

    private void initUI() {
        tvZhuzhan = (TextView) findViewById(R.id.tv_dialog_novel_create_publish_zhuzhan);
        tvShoufa = (TextView) findViewById(R.id.tv_dialog_novel_create_publish_shoufa);
        imgZhuzhan = (ImageView) findViewById(R.id.img_dialog_novel_create_publish_zhuzhan);
        imgShoufa = (ImageView) findViewById(R.id.img_dialog_novel_create_publish_shoufa);
        etTongren = (EditText) findViewById(R.id.et_dialog_novel_create_original);

        rlZhuzhan = (RelativeLayout) findViewById(R.id.rl_dialog_novel_create_publish_zhuzhan);
        rlZhuzhan.setOnClickListener(this);
        rlShoufa = (RelativeLayout) findViewById(R.id.rl_dialog_novel_create_publish_shoufa);
        rlShoufa.setOnClickListener(this);

        btnCancel = (Button) findViewById(R.id.btn_dialog_novel_create_publish_cancel);
        btnCancel.setOnClickListener(this);
        btnSure = (Button) findViewById(R.id.btn_dialog_novel_create_publish_sure);
        btnSure.setOnClickListener(this);

        imgHelper = (ImageView) findViewById(R.id.img_dialog_novel_create_original_help);
        imgHelper.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_dialog_novel_create_publish_zhuzhan:
                selectPublish(1);
                break;
            case R.id.rl_dialog_novel_create_publish_shoufa:
                selectPublish(2);
                break;
            case R.id.btn_dialog_novel_create_publish_cancel:
                dismiss();
                break;
            case R.id.btn_dialog_novel_create_publish_sure:
                if(1 == typePublish){
                    name = "原创";
                }else if(2 == typePublish){
                    name = "同人";
                    if(TextUtils.isEmpty(etTongren.getText().toString().trim())){
                        UIUtil.toastMessage(context, "请填写同人来源");
                        return;
                    }
                }else{
                    UIUtil.toastMessage(context, "请选择原创/同人");
                    return;
                }
                if(3 == from){
                    if(waitDialog != null && !waitDialog.isShowing()){
                        waitDialog.show();
                    }
                    if(bean!=null){
                        NovelCreateServices.getInstance().saveTopicNovel(this, bean.getId(), novelType, null, typePublish+"", etTongren.getText().toString(), null, null, null);
                    }
                }else {
                    listener.selectOriginalListener(name, typePublish, etTongren.getText().toString());
                    dismiss();
                }
                break;
            case R.id.img_dialog_novel_create_original_help:
                BrowseActivity.startActivity(context, "http://api.doufu.la/index.php/journal/detail?id=63");
                break;
            default:
                break;
        }
    }

    private void selectPublish(int type){
        tvZhuzhan.setTextColor(context.getResources().getColor(R.color.novel_create_text));
        tvShoufa.setTextColor(context.getResources().getColor(R.color.novel_create_text));
        imgZhuzhan.setVisibility(View.INVISIBLE);
        imgShoufa.setVisibility(View.INVISIBLE);
        typePublish = type;
        switch (type){
            case 1:
                etTongren.setVisibility(View.GONE);
                tvZhuzhan.setTextColor(context.getResources().getColor(R.color.cfe6e6e));
                imgZhuzhan.setVisibility(View.VISIBLE);
                break;
            case 2:
                etTongren.setVisibility(View.VISIBLE);
                tvShoufa.setTextColor(context.getResources().getColor(R.color.cfe6e6e));
                imgShoufa.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        if(waitDialog != null && waitDialog.isShowing()){
            waitDialog.dismiss();
        }
        try {
            if(response.getStatus() == Constant.STATUS_SUCCESS){
                UIUtil.toastMessage(context, "修改成功");
                listener.selectOriginalListener(name, typePublish, etTongren.getText().toString());
                dismiss();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
